package com.ggj2013.hbh;

import nme.display.Sprite;
import nme.Lib;
import nme.events.Event;

/**
 * @author Jonathan Moriarty
 */
class HBH extends Sprite {
	public static var menuScreen:MenuScreen;
	public static var levelScreen:LevelScreen;
	//public static var creditsScreen:CreditsScreen;
	
	public static var inputMan:InputManager;
	
	public static var STAGE_WIDTH:Float = 800.0;
	public static var STAGE_HEIGHT:Float = 600.0;
	
	public function new() {
		super();
		
		inputMan = new InputManager( this );
		
		onRequestMenu();
		Lib.current.stage.addEventListener( Event.ENTER_FRAME, update );
	}
	
	public function clearScreens():Void
	{
		clearMenu();
		clearLevel();
	}
	
	public function clearMenu():Void
	{
		if ( HBH.menuScreen != null )
		{

			removeChild( menuScreen );
			menuScreen.removeEventListener( NavigationEvent.LEVEL, onRequestLevel );
			//menuScreen.removeEventListener( NavigationEvent.CREDITS, onRequestCredits );
			menuScreen = null;
		}
	}
	
	/*public function clearCredits():Void
	{
		if ( creditsScreen != null )
		{
			creditsScreen.reset();
			creditsScreen.hide();
			removeChild( creditsScreen );
			creditsScreen.removeEventListener( NavigationEvent.MENU, onRequestMenu );
			creditsScreen = null;
		}
	}*/
	
	public function clearLevel():Void
	{
		if ( levelScreen != null )
		{
			removeChild( levelScreen );
			levelScreen.removeEventListener( NavigationEvent.LEVEL, onRequestLevel );
			levelScreen.removeEventListener( NavigationEvent.MENU, onRequestMenu );
			levelScreen = null;
		}
	}
	
	public function onRequestMenu( l_navEvent:NavigationEvent = null ):Void
	{
		clearScreens();
		Lib.current.stage.stageFocusRect = false;
		menuScreen = new MenuScreen();
		//menuScreen.addEventListener( NavigationEvent.CREDITS, onRequestCredits );
		menuScreen.addEventListener( NavigationEvent.LEVEL, onRequestLevel );
		menuScreen.x = 0;
		menuScreen.y = 0;
		addChild( menuScreen );
		Lib.current.stage.focus = menuScreen;
	}
	
	public function onRequestLevel( l_navEvent:NavigationEvent = null ):Void
	{
		clearScreens();
		
		Lib.current.stage.stageFocusRect = false;
		levelScreen = new LevelScreen();
		levelScreen.addEventListener( NavigationEvent.LEVEL, onRequestLevel );
		levelScreen.addEventListener( NavigationEvent.MENU, onRequestMenu );
		levelScreen.x = 0;
		levelScreen.y = 0;
		addChild( levelScreen );
		Lib.current.stage.focus = levelScreen;
	}
	
	/*public function onRequestCredits( l_navEvent:NavigationEvent = null ):Void
	{
		clearScreens();
		Lib.current.stage.stageFocusRect = false;
		creditsScreen = new CreditsScreen();
		creditsScreen.addEventListener( NavigationEvent.MENU, onRequestMenu );
		creditsScreen.x = 0;
		creditsScreen.y = 0;
		addChild( creditsScreen );
		Lib.current.stage.focus = creditsScreen;
	}*/
	
	public function update( l_event:Event ):Void 
	{
		if ( levelScreen != null )
		{
			levelScreen.update( l_event );
		}
		if ( menuScreen != null )
		{
			menuScreen.update( l_event );
		}
		InputManager.update();
	}
	
	
	public static function main() {
		Lib.current.addChild( new HBH() );
	}	

}
